package com.tongfusoft.document.manager.role.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.RoleHasPrivileges;
import com.tongfusoft.document.entity.RoleHasSecurities;
import com.tongfusoft.document.entity.RoleHasUsers;
import com.tongfusoft.document.entity.Roles;
import com.tongfusoft.document.entity.SecurityGroup;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.role.service.IDocumentRoleService;
import com.tongfusoft.document.manager.security.service.IDocumentSecurityGroupService;
import com.tongfusoft.document.manager.user.service.IDocumentUserService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("DocumentRoleService")
public class DocumentRoleServiceImpl extends DataTemplateServiceImpl
		implements IDocumentRoleService {

	@Autowired
	@Qualifier(value="DocumentRoleService")
	public IDocumentRoleService roleService;
	@Autowired
	@Qualifier(value="documentSecurityGroupService")
	public IDocumentSecurityGroupService groupService;
	@Autowired
	@Qualifier(value = "DocumentUserService")
	public IDocumentUserService userService;
	
	
	/**
	 * 角色列表(分页)
	 * */
	@Override
	public PageInfo<Roles> rolePageTable(int pageIndex, int pageSize,
			Roles roles) {
		return super.findPage("roleList", roles, pageSize, pageIndex);
	}
	
	/**
	 * 对应角色所关联的用户列表(分页)
	 */
	@Override
	public PageInfo<RoleHasUsers> getUserHasRole(int pageIndex, int pageSize,RoleHasUsers roleHasUsers) {
		return super.findPage("getUserHasRole", roleHasUsers, pageSize, pageIndex);
	}
	
	/**
	 * 创建角色
	 * */
	@Override
	public void createRole(Roles roles, Users user) {
		if (super.selectList("roleList", roles).size() > 0) {
			throw new ServiceException("repeat_error");
		}

		roles.setRoleId(UIDGeneratorService.getUUID());
		roles.setStatus("1");
		roles.setCreatedBy(user.getUserId());
		roles.setCreatedDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		roles.setLastUpdBy(user.getUserId());
		roles.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		
		SecurityGroup group = new SecurityGroup();
		List<SecurityGroup> allGroup = this.groupService.groupList(group);
		RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
		for (int i = 0; i < allGroup.size(); i++) {
			roleHasSecurities.setGroupId(allGroup.get(i).getGroupId());
			roleHasSecurities.setRoleId(roles.getRoleId());
			this.roleService.addRoleHasGroup(roleHasSecurities,user);
		}
		super.insert("addRole",roles);
	}
	
	/**
	 * 角色与安全组绑定
	 * */
	@Override
	public void addRoleHasGroup(RoleHasSecurities roleHasSecurities,Users user) {

		roleHasSecurities.setRpId(UIDGeneratorService.getUUID());
		roleHasSecurities.setReadPower("0");
		roleHasSecurities.setDeletePower("0");
		roleHasSecurities.setUpdatePower("0");
		roleHasSecurities.setManagerPower("0");
		roleHasSecurities.setCreatedBy(user.getUserId());
		roleHasSecurities.setCreatedDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		roleHasSecurities.setStatus("1");
		this.insert("addRoleHasGroup", roleHasSecurities);
	}
	
	/**
	 * 查询角色是否绑定用户
	 */
	@Override
	public List<RoleHasUsers> getRoleHasUser(RoleHasUsers roleHasUsers) {
		return this.selectList("getRoleHasUsers", roleHasUsers);
	}
	/**
	 * 删除角色
	 * */
	@Override
	public void delRole(Roles role, Users user) {
		RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
		roleHasSecurities.setRoleId(role.getRoleId());
		this.roleService.delRoleHasGroup(roleHasSecurities,user);

		role.setLastUpdBy(user.getUserId());
		role.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("delRole", role);
	}
	
	/**
	 * 删除角色对应权限
	 * */
	@Override
	public void delRoleHasGroup(RoleHasSecurities roleHasSecurities, Users user) {
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("delRoleHasGroup", roleHasSecurities);
	}

	/**
	 * 角色详情
	 * */
	@Override
	public Roles getRole(Roles role,Users user) {
		return (Roles) this.selectOne("getRole", role);
	}
	
	/**
	 * 编辑角色
	 * */
	@Override
	public void updateRole(Roles roles, Users user) {
		if (super.selectList("roleListOutSelf", roles).size() > 0) {
			throw new ServiceException("repeat_error");
		}
		roles.setLastUpdBy(user.getUserId());
		roles.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateRole", roles);
	}
	

	/**
	 * 角色列表
	 * */
	@Override
	public List<Roles> roleList(Roles role) {
		return this.selectList("roleList",role);
	}
	/**
	 * 根据安全组ID来查询其对应权限
	 * */
	@Override
	public RoleHasSecurities powerByRpId(String rpId) {
		return (RoleHasSecurities) this.selectOne("allRoleHasSecurities",rpId);
	}
	
	/**
	 * 角色列表(用于用户绑定角色)
	 * */
	@Override
	public List<Roles> roleListById(Users user) {
		return this.selectList("getRolesByUserId",user);
	}
	/**
	 * 用户列表(用于角色绑定用户)
	 * */
	@Override
	public List<Users> userListById(Roles roles) {
		return this.selectList("getUsersByRoleId",roles);
	}
	

	/**
	 * 添加角色和用户关联
	 */
	public void saveUsersHasRole(String[] userList,String roleId,Users user){
		RoleHasUsers ru = new RoleHasUsers();
		for (String userId : userList) {
			if(userId.equals("")){	
			}else{
				ru.setUserId(userId);
				ru.setRoleId(roleId);
				this.userService.createRoleHasUser(ru,user);
			}
		}
	}
	
	
	/*
	 * 角色安全组权限操作
	 * */
	/**
	 * 修改对应角色与安全组权限
	 * */
	@Override
	public void updateRoleHasGroup(RoleHasSecurities roleHasSecurities,Users user) {
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateRoleHasGroup", roleHasSecurities);
	}
	/**
	 * 角色与安全组权限管理表
	 * */
	@Override
	public PageInfo<RoleHasSecurities> roleHasSecuritiesPageTable(
			int pageIndex, int pageSize, RoleHasSecurities roleHasSecurities){
		return super.findPage("RoleHasGroupList", roleHasSecurities, pageSize, pageIndex);
	}
}
